# Create a database instance, and connect to it.
import asyncio
import contextvars
from urllib.parse import quote

from databases import Database


database = Database(f'mysql://root:{quote("kexin@kexin2021")}@192.168.1.80/learn_db')


async def main():
    await database.connect()

    query = "SELECT * FROM HighScores limit 1"
    query2 = 'SELECT * FROM HighScores limit 3'

    await database.fetch_all(query)

    async def task1():
        print('task1 start---')
        await asyncio.sleep(4)
        res = await database.fetch_all(query)
        print('task1', res)

    async def task2():
        print('task2 start---')
        async for i in database.iterate(query2):
            await asyncio.sleep(2)
            print('task2->', i)

    await asyncio.gather(task1(), task2())


    await database.disconnect()

asyncio.run(main())
